package cn.itmantzy.pet.controller;

import cn.itmantzy.basic.util.AjaxResult;
import cn.itmantzy.basic.util.LoginContext;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.pet.domain.Pet;
import cn.itmantzy.pet.query.PetQuery;
import cn.itmantzy.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        PageList<Pet> pets = petService.queryPage(query);
        return pets;
    }

    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    // 根据宠物id查询宠物相关信息和宠物详情
    @GetMapping("{id}")
    public Pet findById(@PathVariable("id") Long id){
        return petService.queryByIdDetail(id);
    }

    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            petService.adopt(id, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后再试");
        }
    }

}
